var app = getApp()
Page({
    data: {
        errorMsg:'',
        fullName: '',
        cellphone: '',
        verifiedCode: '',
        city: '',
        userCode: '',
        cityArray: ['北京', '上海', '成都', '青岛', '济南', '郑州', '南京', '西安', '合肥', '武汉', '苏州', '石家庄'],
        index: 0,
        openId: ''
    },
    bindPickerCity: function bindPickerCity(e) {
        this.setData({
            index: e.detail.value
        });
    },
    bindCellphoneValue:function(e) {
        this.setData({
            cellphone: e.detail.value
        })
    },
    sendcode: function sendcode() {        
        var that = this;
        wx.request({
            url: 'http://localhost:8080/wxAppletContact/sendVerifiedCode',
            data: {
                cellphone: that.data.cellphone,
                operation: "Register"
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function success(res) {
                if(res.data.status == 200){
                   
                     wx.showToast({
                    title: '验证码发送成功',
                    icon: 'success',
                    duration: 2000
                    });  
                }
                if(res.data.errorCode == 2000){
                   
                    wx.showModal({
                        showCancel:false,
                        content: res.data.errorMessage,
                    })
                }
                if(res.data.errorCode == 1010){
                    wx.showToast({
                    title: res.data.errorMessage,
                    icon: 'loading',
                    duration: 2000
                    });
                }
                 setTimeout(function() {
                    that.setData({
                         errorMsg:''
                     })
                }, 3000);
            },
            fail: function fail(res) {
                
            },
            complete: function complete() {
            }
        });
    },
    register: function register(e) {
        var that = this;        
        wx.request({
            url: 'http://localhost:8080/wxAppletContact/register',
            data: {
                fullName: e.detail.value.fullName,
                cellphone: e.detail.value.cellphone,
                verifiedCode: e.detail.value.verifiedCode,
                city: e.detail.value.city,
                userCode: e.detail.value.userCode,
                openId: that.data.openId
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function success(res) {
                if(res.data.status == 200){
                    wx.navigateTo({
                    url: 'registerSuccess'
                    }) 
                }
                
            },
            fail: function fail(res) {                
                console.log(res)
            },
            complete: function complete(res) {
                console.log(res)
            }
        });
    },
    onLoad: function(options) {
        this.setData({
            openId: app.globalData.openId
        });
        wx.login({
            success: function(res) {
                if (res.code) {
                    console.log(res.code)
                    // wx.request({
                    //     url: 'http://localhost:8080/wxAppletContact/getOpenId',
                    //     data: {
                    //         code: res.code
                    //     },
                    //     method: 'POST',
                    //     header: {
                    //         'content-type': 'application/json'
                    //     },
                    //     success: function(res) {
                    //       console.log("sssssssssss")
                    //       try {
                    //           rageSync('openid', res.openid)
                    //       } catch (e) {
                    //       }
                    //     },
                    //     fail: function() {                          
                    //     },
                    //     complete: function() {                          
                    //     }
                    // })
                } else {
                    console.log('获取用户登录态失败！' + res.errMsg)
                }
            }
        });        
    }
});